<template>
    <div>
        <el-card style="width:50%">
            <el-form :model="user" label-width="80px" style="padding-right:30px">
                <div style="margin:15px;text-align:center">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:9090/file/upload"
                        :show-file-list="false"
                        :headers="{token: user.token}"
                        :on-success="handleAvatarSuccess">
                        <img v-if="user.avatar" :src="user.avatar" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </div>
                <el-form-item label="账号" prop="count">
                    <el-input v-model="user.count" disabled id="count"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="user.name" id="name"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="user.phone" id="phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="user.email" id="email"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input type="textarea" v-model="user.address" id="address"></el-input>
                </el-form-item>
                <div style="text-align:center">
                    <el-button type="primary" @click="update">保存</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default{
    name:"Person",
    data(){
        return {
            user:JSON.parse(localStorage.getItem('honey-user') || '{}')
        }
    },
    created(){

    },
    methods:{
        update(){
            //保存当前用户信息到数据库
            this.$request.put('/user/update',this.user).then(res=>{
                if(res.code==='200'){
                    this.$message.success('保存成功')
                    //更新浏览器缓存里的用户信息
                    localStorage.setItem('honey-user',JSON.stringify(this.user))
                    //触发父级的数据更新
                    this.$emit('update:user',this.user)
                }else{
                    this.$message.error(res.msg)
                }
            })
        },
        handleAvatarSuccess(response,file,fileList){
            this.user.avatar=response.data
        },

    }
}
</script>

<style scoped>
.el-form-item__label{
    font-weight:bold;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-radius: 50%;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 50%;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
}
</style>